package office.service.finance.impl;

import office.bo.hr.impl.CheckFolw;
import office.service.finance.ICheckFolwService;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public abstract class CheckFolwServiceImpl extends BaseBizServiceImpl implements ICheckFolwService{
	/**
	 * 根据id获取对象
	 * @return
	 * @throws BusinessException
	 */
	public abstract CheckFolw getCheckFolwById(String id)throws BusinessException;
	
	/**
	 * 提交审批
	 * @param id
	 * @throws BusinessException
	 */
	public void submitCheck(String id,String proKey) throws BusinessException{
		try {
			CheckFolw checkFolw = this.getCheckFolwById(id);
			checkFolw.setState("1");
			String nextRole = getNextCheckRole(checkFolw.getEntryRole(),checkFolw.getCheckRole(),proKey);
			checkFolw.setCheckRole(nextRole);
			checkFolw.store(domainProviderSet);//保存对象
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("提交审批失败!", e);
		}
	}

	/**
	 * 
	 * @param check 审批是否为通过
	 * @param id
	 * @throws BusinessException
	 */
	public void checkData(boolean check,String id,String proKey)throws BusinessException{
		try {
			CheckFolw checkFolw = this.getCheckFolwById(id);
			if(check){
				String nextRole = getNextCheckRole(checkFolw.getEntryRole(),checkFolw.getCheckRole(),proKey);
				if(nextRole.equals("end")){
					checkFolw.setState("100");
					checkFolw.setCheckRole("cwcn");
				}else{
					checkFolw.setCheckRole(nextRole);
				}
			}else{
				checkFolw.setCheckRole("");//重新提交审批,CheckRole重新赋值
				checkFolw.setState("-1");//审批驳回
			}
			checkFolw.store(domainProviderSet);//保存对象
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("设置审批状态信息对象失败!", e);
		}
	}
	/**
	 * 
	 * @param key 配置文件key
	 * @param preRole 现在所处审批角色
	 * @return
	 * @throws BusinessException
	 */
	public String getNextCheckRole(String entryRole,String preRole,String proKey)throws BusinessException{
		String roles = Util.parsePropertiesReturnStr("office", proKey+"-"+entryRole);
		String role[] = roles.split(",");
		if(null==preRole || ("").equals(preRole)){
			if(role.length<2){
				throw new BusinessException("未找到下级审批节点,审批流程配配置错误");
			}
			return role[1];
		}
		if(roles.indexOf(",")>-1){
			if(roles.endsWith(","+preRole)){
				return "end";
			}else if(roles.endsWith(preRole)){
				return "end";
			}
		}
		
		for(int i=0; i<role.length;i++){
			if(role[i].equals(preRole)){
				return role[i+1];
			}
		}
		return "";
	}
}
